package com.lsm.test;
import com.lsm.entity.Product;
import com.lsm.entity.Product;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;


public class MybatisProductTest2 {

    SqlSession sqlSession;
    @Before
    public void beforeTest() throws IOException {
        //加载核心配置文件SqlMapConfig.xml
        InputStream is=Resources.getResourceAsStream("SqlMapConfig.xml");
        //获取SqlSession工厂对象
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
        //获取SqlSession对象
        sqlSession=sqlSessionFactory.openSession(true);
    }


    //新增
    @Test
    public void testAddProduct() throws IOException {

        //创建Product对象
        Product p=new Product();
        p.setProductNum("9528");
        p.setProductName("太湖三节课游");
        p.setCityName("太湖3");
        p.setDepartureTime(new Date());
        p.setProductPrice(1000.0);
        p.setProductDesc("无锡太湖学院真美丽3");
        p.setProductStatus(1);
        //执行插入，把Product插入数据库表product中
        sqlSession.insert("productMapper.addProduct",p);
        //此时不需要手动提交事务，会自动提交事务
    }

    @Test
    public void queryProductList() throws IOException{
        //获取查询
        List<Product> list= sqlSession.selectList("queryProductAll");
        //System.out.println(list);
        //遍历集合
        for(Product product:list){
            System.out.println(product);
        }
    }
    @After
    public void afterTest(){
        //释放资源
        sqlSession.close();
    }
}
